<?php
declare (strict_types=1);

namespace app\common\model;

use addons\Target\facade\Target;

class Draw extends Base
{
    protected $name = 'draw';

    protected $type = [
        'confirm_time' => 'timestamp',
    ];

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile');
    }

    public function getAccountAttr($value)
    {
        return get_pay_way($value);
    }

    public function getStatusAttr($value)
    {
        return match ($value) {
            1 => '待审核',
            2 => '已通过',
            3 => '已驳回',
            default => ''
        };
    }

    public static function _save($param, $logs)
    {

        parent::startTrans();
        try {
            if (!isset($param['status'])) return ['code' => -1, 'msg' => '请选择审核类型'];

            $info = parent::find($param['id']);
            if (empty($info)) return ['code' => -1, 'msg' => '审核失败'];
            if ($info->getData('status') != 1) {
                return ['code' => -1, 'msg' => '此笔记录已审核，请勿重复审核！'];
            }
            if ($param['status'] == 2) {
                $msg = '通过提现，ID【' . $param['id'] . '】';
            } else {
                $msg = '提现驳回,ID【' . $param['id'] . '】';
                Target::Account()->basics($info['uid'], $info->getData('account'), $info['money'], $info['money'], '提现驳回', '+')
                    ->createNormal();
            }

            $info->status = $param['status'];
            if ($param['status'] != 2) {
                $info->reject_msg = $param['reject_msg'];
            }
            $info->confirm_time = time();
            $info->admin_id = $logs['admin']->id;
            $info->admin_name = $logs['admin']->admin_name;
            $info->save();
            Logs::adds($msg, $logs['admin']->id, $logs['admin']->admin_name);
            parent::commit();
            return ['code' => 1, 'msg' => '已审核'];

        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function batch($ids, $admin)
    {
        parent::startTrans();
        try {
            $infos = parent::where('status', 1)->whereIn('id', $ids)->select();
            if ($infos->isEmpty()) {
                return ['code' => -1, 'msg' => '未查询到待审核的数据，请检查！'];
            }
            $newIds = [];
            foreach ($infos as $v) {
                $newIds[] = $v->id;
            }
            parent::update(['status' => 2, 'admin_id' => $admin->id, 'admin_name' => $admin->admin_name], ['id' => $newIds]);
            parent::commit();
            $msg = '批量通过提现申请,标识ID【' . implode(',', $ids) . '】';
            Logs::adds($msg, $admin['id'], $admin['admin_name']);
            return ['code' => 1, 'msg' => '已批量审核'];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function add($param, $user, $system)
    {
        try {

            if (!$system['draw_open']) {
                return ['code' => -1, 'msg' => '暂时无法提现'];
            }
            $account = $param['account'];

            $week = explode(',', $system['draw_week']);
            if (!in_array(date('w'), $week)) {
                return ['code' => -1, 'msg' => '每周' . $system['draw_week'] . '可提现'];
            }
            $times = explode(',', $system['draw_times']);
            $his = date('H:i:s');
            if ($his < $times[0] || $his > $times[1]) {
                return ['code' => -1, 'msg' => '提现时间为：' . $times[0] . ' ~ ' . $times[1]];
            }

            $count = parent::where('uid', $user->id)->whereTime('create_time', 'today')->count();
            if ($count >= $system['draw_day']) {
                return ['code' => -1, 'msg' => '每日只可提现' . $system['draw_day'] . '次'];
            }

            if (!$param['money'] || $param['money'] <= 0) {
                return ['code' => -1, 'msg' => '提现金额错误'];
            }

            if ($param['money'] < $system['draw_min']) {
                return ['code' => -1, 'msg' => '最低提现金额：' . $system['draw_min']];
            }

            if ($system['draw_bs']) {
                if ($param['money'] % $system['draw_bs'] != 0) {
                    return ['code' => -1, 'msg' => '提现倍数：' . $system['draw_bs']];
                }
            }

            if ($user->assets->$account < $param['money']) {
                return ['code' => -1, 'msg' => '余额不足'];
            }
            if (md5($param['pay_password']) != $user['pay_password']) {
                return ['code' => -1, 'msg' => '支付密码错误'];
            }
            $cost = 0;
            if ($system['draw_cost'] && $system['draw_cost'] > 0) {
                $cost = round($param['money'] * $system['draw_cost'], 2);
            }

            switch ($param['mode']) {
                case "微信":
                    $create['wechat_name'] = $user->wechat_name;
                    $create['wechat_image'] = $user->wechat_image;
                    break;
                case "支付宝":
                    $create['alipay_name'] = $user->alipay_name;
                    $create['alipay_image'] = $user->alipay_image;
                    break;
                default:
                    $create['bank_name'] = $user->bank_name;
                    $create['bank_open'] = $user->bank_open;
                    $create['bank_account_name'] = $user->bank_account_name;
                    $create['bank_number'] = $user->bank_number;

            }
            $create['uid'] = $user->id;
            $create['account'] = $account;
            $create['mode'] = $param['mode'];
            $create['money'] = $param['money'];
            $create['cost'] = $cost;
            $create['amount'] = $param['money'] - $cost;

            parent::create($create);

            Target::Account()->basics($user->id, $account, $param['money'],$param['money'], '提现', '-')->createNormal();

            return ['code' => 1, 'msg' => '申请已提交，请耐心等待审核'];

        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
